package com.www.ve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.www.ve.dao.SysUserDao;
import com.www.ve.dto.SysLoginDTO;
import com.www.ve.entitys.SysUserEntity;
import com.www.ve.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Override
    public Boolean removeUser(String userId) {
        baseMapper.updateByUserId(userId);
        return true;
    }

    @Override
    public Boolean register(SysLoginDTO register) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        BeanUtil.copyProperties(register,sysUserEntity);
        sysUserEntity.setPassword(MD5.create().digestHex(MD5.create().digestHex(register.getPassword())));
        return baseMapper.insert(sysUserEntity)>0;
    }

    @Override
    public Boolean login(SysLoginDTO login) {
        LambdaUpdateWrapper<SysUserEntity> wrapper = Wrappers.lambdaUpdate(new SysUserEntity());
        wrapper.eq(SysUserEntity::getUsername,login.getUsername());
        SysUserEntity user = baseMapper.selectOne(wrapper);
        String pas = MD5.create().digestHex(MD5.create().digestHex(login.getPassword()));
        return StringUtils.equals(user.getPassword(), pas);
    }

}
